<!--
 * @Author: H.
 * @Date: 2021-12-06 18:12:03
 * @LastEditTime: 2021-12-14 13:55:48
 * @Description:
-->
<template>
  <div class="user">
    <page-search
      :searchFormConfig="searchFromConfig"
      @queryBtnClick="handleQueryClick"
      @resetBtnClick="handleResetClick"
    />
    <page-content
      pageName="users"
      ref="pageContentRef"
      @newBtnClick="handleNewData"
      @editBtnClick="handleEditData"
      :contentTableConfig="contentTableConfig"
    >
      <template #enable="scope">
        <el-tag :type="scope.row.enable ? 'success' : 'danger'">{{
          scope.row.enable ? '启用' : '禁用'
        }}</el-tag>
      </template>
    </page-content>
    <page-modal
      ref="pageModalRef"
      :pageModalTitle="pageModalTitle"
      :pageModalConfig="modalConfigRef"
      pageName="users"
      :defaultInfo="defaultInfo"
    ></page-modal>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue'

// 组件
import PageSearch from '@/components/page-search'
import PageContent from '@/components/page-content'
import PageModal from '@/components/page-modal'
// 组件的配置
import { searchFromConfig } from './config/search.config'
import { contentTableConfig } from './config/content.config'
import { pageModalConfig } from './config/modal.config'
// 组件的hook
import { usePageSearch } from '@/hooks/usePageSearch'
import { usePageModal } from '@/hooks/usePageModal'

import { useStore } from '@/store'
export default defineComponent({
  setup() {
    const [pageContentRef, handleResetClick, handleQueryClick] = usePageSearch()
    const pageModalTitle = ref('')

    // pageModal相关的hook逻辑

    // 1.处理密码和pageModalTitle的逻辑
    const newCallback = () => {
      pageModalTitle.value = '新增用户'
      const passwordItem = pageModalConfig.formItems.find((item) => {
        return item.field === 'password'
      })
      passwordItem!.isHidden = false
    }
    const editCallback = () => {
      pageModalTitle.value = '编辑用户'
      const passwordItem = pageModalConfig.formItems.find((item) => {
        return item.field === 'password'
      })
      passwordItem!.isHidden = true
    }

    // 2.动态添加部门和角色列表
    const store = useStore()
    const modalConfigRef = computed(() => {
      const departmentItem = pageModalConfig.formItems.find(
        (item) => item.field === 'departmentId'
      )
      departmentItem!.options = store.state.entireDepartment.map(
        (item: any) => {
          return { label: item.name, value: item.id }
        }
      )
      const roleItem = pageModalConfig.formItems.find(
        (item) => item.field === 'roleId'
      )
      roleItem!.options = store.state.entireRole.map((item: any) => {
        return { label: item.name, value: item.id }
      })

      return pageModalConfig
    })

    // 3.调用hook获取公共变量和函数
    const [pageModalRef, defaultInfo, handleNewData, handleEditData] =
      usePageModal(newCallback, editCallback)

    return {
      searchFromConfig,
      contentTableConfig,
      handleQueryClick,
      handleResetClick,
      pageContentRef,
      modalConfigRef,
      handleNewData,
      handleEditData,
      pageModalRef,
      defaultInfo,
      pageModalTitle
    }
  },
  components: {
    PageSearch,
    PageContent,
    PageModal
  }
})
</script>

<style lang="scss" scoped></style>
